package cn.iocoder.yudao.module.book.service.app;

import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.module.book.controller.app.vo.AssignmentsByClassIdRespVO;
import cn.iocoder.yudao.module.book.controller.app.vo.BookSchoolAssignmentSaveReqVO;
import cn.iocoder.yudao.module.book.controller.app.vo.BookSchoolAssignmentSaveRespVO;
import cn.iocoder.yudao.module.book.dal.dataobject.BookClassAndSchoolDO;
import cn.iocoder.yudao.module.book.dal.dataobject.BookClassManagementMemberDO;
import cn.iocoder.yudao.module.book.dal.dataobject.BookSchoolAssignmentDO;
import cn.iocoder.yudao.module.book.dal.mysql.BookClassAndSchoolMapper;
import cn.iocoder.yudao.module.book.dal.mysql.BookClassManagementMemberMapper;
import cn.iocoder.yudao.module.book.dal.mysql.BookSchoolAssignmentMapper;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BookSchoolAssignmentServiceImpl implements BookSchoolAssignmentService {

    @Resource
    private BookSchoolAssignmentMapper bookSchoolAssignmentMapper;
    @Resource
    private BookClassAndSchoolMapper bookClassAndSchoolMapper;

    @Resource
    private BookClassManagementMemberMapper bookClassManagementMemberMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createBookSchoolAssignment(BookSchoolAssignmentSaveReqVO bookSchoolAssignmentSaveReq) {
        bookSchoolAssignmentSaveReq.getClassIds().forEach(classManagementId -> {
            BookSchoolAssignmentDO assignment = new BookSchoolAssignmentDO();
            assignment.setJobName(bookSchoolAssignmentSaveReq.getJobName());
            assignment.setJobTime(bookSchoolAssignmentSaveReq.getJobTime());
            assignment.setClassManagementId(classManagementId);
            assignment.setJobCompletionConditions(bookSchoolAssignmentSaveReq.getJobCompletionConditions());
            assignment.setBookIds(bookSchoolAssignmentSaveReq.getBookIds());
            bookSchoolAssignmentMapper.insert(assignment);
        });
    }

    @Override
    public List<BookSchoolAssignmentSaveRespVO> getBookSchoolAssignmentList() {
        List<BookSchoolAssignmentDO> bookSchoolAssignmentDOList = bookSchoolAssignmentMapper.selectList();

        return List.of();
    }

    @Override
    public void updateBookSchoolAssignment(BookSchoolAssignmentSaveReqVO bookSchoolAssignmentSaveReq) {
        BookSchoolAssignmentDO bookSchoolAssignmentDO = BeanUtils.toBean(bookSchoolAssignmentSaveReq, BookSchoolAssignmentDO.class);
        bookSchoolAssignmentMapper.updateById(bookSchoolAssignmentDO);
    }

    @Override
    public void deleteBookSchoolAssignment(Long id) {
        bookSchoolAssignmentMapper.deleteById(id);
        bookClassAndSchoolMapper.delete(new QueryWrapperX<BookClassAndSchoolDO>().eq("book_id", id));
    }

    @Override
    public List<AssignmentsByClassIdRespVO> getAssignmentsByClassId(String classId) {
        LambdaQueryWrapper<BookSchoolAssignmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BookSchoolAssignmentDO::getClassManagementId, classId);
        queryWrapper.orderByDesc(BookSchoolAssignmentDO::getCreateTime);
        List<BookSchoolAssignmentDO> bookSchoolAssignmentDOList = bookSchoolAssignmentMapper.selectList(queryWrapper);
        List<AssignmentsByClassIdRespVO> assignmentsByClassIdRespVOS = BeanUtil.copyToList(bookSchoolAssignmentDOList, AssignmentsByClassIdRespVO.class);
        if (CollUtil.isNotEmpty(assignmentsByClassIdRespVOS)) {
            assignmentsByClassIdRespVOS.forEach(vo -> {
                LambdaQueryWrapper<BookClassManagementMemberDO> memberQueryWrapper = new LambdaQueryWrapper<>();
                memberQueryWrapper.eq(BookClassManagementMemberDO::getClassManagementId, classId);
                vo.setClassMemberCount(String.valueOf(bookClassManagementMemberMapper.selectCount(memberQueryWrapper)));
            });
        }
        return assignmentsByClassIdRespVOS;
    }

    @Override
    public AssignmentsByClassIdRespVO getAssignmentDetailsById(String id) {
        AssignmentsByClassIdRespVO vo = new AssignmentsByClassIdRespVO();
        BookSchoolAssignmentDO bookSchoolAssignmentDO = bookSchoolAssignmentMapper.selectById(id);
        if (Objects.nonNull(bookSchoolAssignmentDO)) {
            LambdaQueryWrapper<BookClassManagementMemberDO> memberQueryWrapper = new LambdaQueryWrapper<>();
            vo = BeanUtil.toBean(bookSchoolAssignmentDO, AssignmentsByClassIdRespVO.class);
            memberQueryWrapper.eq(BookClassManagementMemberDO::getClassManagementId, bookSchoolAssignmentDO.getClassManagementId());
            vo.setClassMemberCount(String.valueOf(bookClassManagementMemberMapper.selectCount(memberQueryWrapper)));
        }
        return vo;
    }
}